<?php
session_start();  

$con = mysql_connect("localhost","haschool_mdl1","B97C0HRYb7");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db_selected = mysql_select_db("haschool_mdl1", $con);
if (!$db_selected) {
    die ('Can\'t use haschool_mdl1 : ' . mysql_error());
}  

$salt = '>;4khn&9g9?3d]{%xuVp@gir{vwp';
$password = $_POST[password];
$username = $_POST[username];	
$parentid = $_POST[parentid];

$firstname = $_POST[firstname];
$lastname = $_POST[lastname];
$email = $_POST[email];
$phone1 = $_POST[phone1];
$phone2 = $_POST[phone2];
$city = $_POST[city];
$address = $_POST[address];
$tosave = $_POST[tosave];

$updateparentq= "update mdl_user set firstname='$firstname', lastname='$lastname', email='$email', phone1='$phone1', 
					phone2='$phone2', city='$city', address='$address' where id=$parentid";

mysql_query($updateparentq);
$studentnames='';
$studentids='';
$arrStudent = array();
$numberStudent = $_POST[theValue] + $_POST[countfixing];	
$i=0;
while($i<=$numberStudent){
	$arrStudent[$i] = array ( 'userid' 		=> $_POST["studentid$i"],
							   'username' 	=> $_POST["username$i"],
							   'password' 	=> md5($_POST["password$i"].$salt),
							   'firstname'	=> $_POST["firstname$i"],
							   'lastname'	=> $_POST["lastname$i"],
							   'email'      => $_POST["email$i"],
							   'gender'     => $_POST["gender$i"],
							   'month'		=> $_POST["month$i"],
							   'day'		=> $_POST["day$i"],
							   'year'		=> $_POST["year$i"],
							   'toremove'	=> $_POST["to_remove$i"]
							   );
	$i = $i+1;
}
$effectiveStudent = 0;
foreach ($arrStudent as $student) {
	if ($student["username"]!=null and $student["username"]!=""){
		if ($student["userid"] == null){
			if ( $student["toremove"]!='true'){
				$sqlUsername= 'select * from mdl_user where username = \''.$student["username"].'\'';
				$result = mysql_query($sqlUsername);
				$num_rows = mysql_num_rows($result);
				if($num_rows>0){
					header( 'Location: http://haschoolmanagement.com/registration/subscription_ldc.php?username='.$username.'&password='.$password.'&unexist=1' ) ;					  
				} 
				else {
					$sqlEmail= 'select * from mdl_user where email = \''.$student["email"].'\'';
					$resultEmail = mysql_query($sqlEmail);
					$num_rows = mysql_num_rows($resultEmail);
					if($num_rows>0){
						header( 'Location: http://haschoolmanagement.com/registration/subscription_ldc.php?username='.$username.'&password='.$password.'&emailexist='.$student["email"] ) ;					  
					}
					else{
						$sql='insert into mdl_user (username, password, firstname, lastname, email, phone1, phone2, city, address)
											values(\''.$student["username"].'\', \''.$student["password"].'\', \''.$student["firstname"].'\', \''.
											$student["lastname"].'\',\''.$student["email"].'\', \''.$student["phone1"].'\', \''.$student["phone2"].
											'\', \''.$student["city"].'\', \''.$student["address"].'\')';
					
						mysql_query($sql);
						$userid = mysql_insert_id();
						
						// insert Birth of date
						$dobnewstudent=$student["month"].'/'.$student["day"].'/'.$student["year"];
						$sqlinfodata="insert into mdl_user_info_data (fieldid, userid, data)
											values(1, $userid, '$dobnewstudent')";
						mysql_query($sqlinfodata);
						
						// insert gender
						$gendernewstudent=$student["gender"];
						$sqlgender="insert into mdl_user_info_data (fieldid, userid, data)
											values(8, $userid, '$gendernewstudent')";
						mysql_query($sqlgender);
							
						// insert parent child relationship
						$sqlcontext = "insert into mdl_context (contextlevel, instanceid, path, depth)
										   values(30, $userid, '/1/30', 2)";
						
						mysql_query($sqlcontext);
						$contextid = mysql_insert_id();
						// insert role asssignment to link the parent with the new kid
						$sqlrole = "insert into mdl_role_assignments(roleid, contextid, userid)
											values(10, $contextid, $parentid)";
						mysql_query($sqlrole);
						$effectiveStudent = $effectiveStudent+1;
						$studentnames = $studentnames.$student["firstname"].', ';
						$studentids = $studentids.$userid.',';
					}
				}
			}
		} else {
			if ( $student["toremove"]=='true'){
				// get contextid
				$contextquery = 'Select contextid from mdl_context where contextlevel=30 and instanceid='.$student["userid"];
				$resultcontexts = mysql_query($contextquery);
				if ($resultcontexts){
					$row = mysql_fetch_array($resultcontexts);
					// delete role assignment
					$sqldeleterole='delete from mdl_role_assignments where roleid =10 and contextid='.$row['contextid'].' and userid='.$parentid;
					mysql_query($sqldeleterole);
				}
				//delete context
				$sqldeletecontext='delete from mdl_context where instanceid ='.$student["userid"];
				mysql_query($sqldeletecontext);
				// Update to deleted status
				$sqluser='update mdl_user set deleted=1 where id ='.$student["userid"];
				mysql_query($sqluser);
				
				$numberStudent= $numberStudent-1;
			} else {
				$sqlupdateuser='update mdl_user set firstname=\''.$student["firstname"].'\', lastname=\''.
							$student["lastname"].'\', email=\''.$student["email"].'\', phone1=\''.$student["phone1"].'\', phone2=\''.
							$student["phone2"].'\', city=\''.$student["city"].'\', address=\''.$student["address"].'\' where id='.$student["userid"];
				mysql_query($sqlupdateuser);
				
				// Update DOB
				$dobolduser=$student["month"].'/'.$student["day"].'/'.$student["year"];
				$sqldobold='update mdl_user_info_data set data =\''.$dobolduser.'\' where userid='.$student["userid"].' and fieldid=1';
				mysql_query($sqldobold);
				
				// Update gender
				$genderolduser=$student["gender"];
				$sqlgenderold='update mdl_user_info_data set data =\''.$genderolduser.'\' where userid='.$student["userid"].' and fieldid=8';
				mysql_query($sqlgenderold);
				
				$effectiveStudent=$effectiveStudent+1;
				$studentnames = $studentnames.$student["firstname"].', ';
				$studentids = $studentids.$student["userid"].',';
			}
		}		
	}
}

if ($tosave==1){
	header( 'Location: http://haschoolmanagement.com/registration/subscription_ldc.php?username='.$username.'&password='.$password ) ;					  
}
if (strlen($studentnames)>2){
	$studentnames= $lastname.' '.substr($studentnames, 0, strlen($studentnames)-2);
}

if(isset($_SESSION['views']) and strlen($studentids)>0){
	$studentids = substr($studentids, 0, strlen($studentids)-2);
    $_SESSION['studentids'] = $studentids;
}
 
mysql_close($con);
?>
<html>
<head>
<title>Horizon Academy Subscription</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="keywords here">
<meta name="description" content="description here">
<meta name="author" content="Joseph De Araujo">
<meta name="publisher" content="interspire.com">
<meta name="robots" content="index, follow"> <!-- (robot commands: all, none, index, no index, follow, no follow) -->
<meta name="revisit-after" content="30 days">
<meta name="distribution" content="global"> 
<meta name="rating" content="general">
<meta name="content-language" content="english">
<script type="text/javascript">
function to_home()
{
    window.location.href='index.php';
}

//-->
</script>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body style="margin:0;">
<table width="747" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="images/spacer.gif" width=747 height=11 alt=""></td>
  </tr>
  <tr>
    <td><img src="images/spacer.gif" width=747 height=11 alt=""></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top" background="images/pixi_lightgrey.gif"><table border=0 cellpadding=0 cellspacing=0 bgcolor="#ffffff">
          <tr bgcolor="#ffffff">
            <td colspan=3><!-- #BeginEditable "mainpic" --><img src="images/mainpic.jpg" width=233 height=164 alt=""><!-- #EndEditable --></td>
          </tr>
          <tr> </tr>
          <tr>
            <td height="56" rowspan=2> <img src="images/picbasecurve_left.jpg" width=15 height=56 alt=""></td>
            <td height="46" align="right" valign="top" bgcolor="#ffffff"> <img src="images/spacer.gif" width=207 height=1 alt=""><br>
              <font size="3" face="verdana, arial, helvetica, sans-serif"></font></td>
            <td height="56" rowspan=2><font size="1" face="verdana, arial, helvetica, sans-serif"><img src="images/picbasecurve_right.jpg" width=11 height=56 alt=""></font></td>
          </tr>
          <tr>
            <td height="10" valign="top" bgcolor="#ffffff"><img src="images/spacer.gif" width=207 height=10 alt=""></td>
          </tr>
        </table></td>
        <td valign="top" background="images/pixi_lightgrey.gif"><table width="516" border=0 cellpadding=0 cellspacing=0>
          <tr>
            <td rowspan=2><img src="images/spacer.gif" width=34 height=252 alt=""></td>
            <td height="36" background="images/pixi_headergrey.gif"> <h1><img src="images/spacer.gif" width=460 height=10 alt=""><br>
&nbsp;&nbsp;&nbsp;&nbsp;Registration Confirmation</h1></td>
          </tr>
          <tr>
            <td height="216" valign="top"><img src="images/spacer.gif" width=460 height=20 alt=""><br>

<?php echo "You need to deposit an amount of $50.00 a child." ; ?>

<?php 
$totaldeposit = $effectiveStudent*50;
$totaldisplay = '$'.$totaldeposit.'.00';
echo "<br /><br />The amount you need to pay is: $totaldisplay.<br /> <br /><b>Please proceed with your payement by clicking on the button below.</b><br /><br />" ; 
?>


<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="UQXTFDH6ZQ78L">
<input type="hidden" name="quantity" value="<?php echo $effectiveStudent;?>">
<input type="hidden" name="item_name" value="<?php echo $studentnames;?>">
<input type="hidden" name="currency_code" value="CAD">
<!--<input type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">-->
<input type="image" src="images/btn_buynowCC_LG2.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/scr/pixel.gif" width="1" height="1">
</form>


<br />

<center><INPUT type=button height=39 name="Home" alt="cliquer" onclick="to_home()" value="Home"></center>
 
            </td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><table width="747" border=0 cellpadding=0 cellspacing=0>
  <tr>
    <td width="15" align="left" valign="top"> <img src="images/basecurve_left.jpg" width=15 height=38 alt=""></td>
    <td background="images/pixi_grey.gif"> <img src="images/spacer.gif" width=218 height=1 alt=""></td>
    <td width="503" valign="top" background="images/pixi_grey.gif" ><img src="images/spacer.gif" alt="" width=504 height=13 hspace="0" vspace="0"><br>
        <span class="baseline" align="center">Copyright 2011 Horizon Academy Institute</span></td>
    <td width="14" align="right" valign="top"><img src="images/basecurve_right.jpg" width=11 height=38 alt=""></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td><img src="images/spacer.gif" width=747 height=11 alt=""></td>
  </tr>
</table>

<script src="form-validation.js"></script> 
</body>
</html>